package com.sskj.qiangjin.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.facebook.stetho.inspector.elements.Document;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.base.HttpData;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserBean;
import com.sskj.lib.mvchelper.refresh.MVCSmartHelper;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.http.HttpConfig;
import com.sskj.qiangjin.presenter.AssetActivityPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.APP_ASSET)//我的资产
public class AssetActivity extends BaseActivity<AssetActivityPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    //    @BindView(R.id.tv_qc_total_money)
//    TextView tvQcTotalMoney;
//    @BindView(R.id.tv_qc_trading_record)
//    TextView tvQcTradingRecord;
//    @BindView(R.id.tv_qc_bill_record)
//    TextView tvQcBillRecord;
//    @BindView(R.id.tv_usdt_total_money)
//    TextView tvUsdtTotalMoney;
//    @BindView(R.id.tv_usdt_trading_record)
//    TextView tvUsdtTradingRecord;
//    @BindView(R.id.tv_usdt_bill_record)
//    TextView tvUsdtBillRecord;
//    @BindView(R.id.btn_usdt_recharge)
//    Button btnUsdtRecharge;
//    @BindView(R.id.btn_usdt_with_draw)
//    Button btnUsdtWithDraw;
    private List<String> moneys = new ArrayList<>();
    private SlimAdapter slimAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_asset;
    }

    @Override
    public AssetActivityPresenter getPresenter() {
        return new AssetActivityPresenter();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mPresenter.getQCInfo();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getQCInfo();
    }

    @Override
    protected void initView() {
        setTitle("我的资产");
//
//        ClickUtil.click(tvQcTradingRecord, () -> ARouter.getInstance().build(RConfig.APP_ASSET_RECORD).withString(Constans.MONEY_TYPE, "QC").navigation());
//        ClickUtil.click(tvQcBillRecord, () -> ARouter.getInstance().build(RConfig.APP_BILLIND_RECORD).withString(Constans.MONEY_TYPE, "QC").navigation());
//
//        ClickUtil.click(tvUsdtTradingRecord, () -> ARouter.getInstance().build(RConfig.APP_ASSET_RECORD).withString(Constans.MONEY_TYPE, "USDT").navigation());
//        ClickUtil.click(tvUsdtBillRecord, () -> ARouter.getInstance().build(RConfig.APP_BILLIND_RECORD).withString(Constans.MONEY_TYPE, "USDT").navigation());
//        ClickUtil.click(btnUsdtRecharge, () -> ARouter.getInstance().build(RConfig.APP_RECHARGE).withString(Constans.MONEY_TYPE, "USDT").navigation());
//        ClickUtil.click(btnUsdtWithDraw, () -> ARouter.getInstance().build(RConfig.APP_WITHDRAW).withString(Constans.MONEY_TYPE, "USDT").navigation());
//

        smartRefreshLayout.setPrimaryColorsId(R.color.app_696, android.R.color.white);
        smartRefreshLayout.setDragRate(0.3f);//显示下拉高度/手指真实下拉高度=阻尼效果
        smartRefreshLayout.setReboundDuration(300);//回弹动画时长（毫秒）
        smartRefreshLayout.setRefreshHeader(new ClassicsHeader(this));//设置Header
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mPresenter.getQCInfo();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_asset, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector injector, List payloads) {
                if (injector.pos() == 2) {
                    injector.visibility(R.id.tv_trading_record, View.GONE)
                            .image(R.id.iv_image_type, App.INSTANCE.getResources().getDrawable(R.mipmap.app_icon_qc))
                            .visibility(R.id.btn_recharge, View.GONE)
                            .visibility(R.id.btn_with_draw, View.GONE)
                            .visibility(R.id.btn_exchange, View.VISIBLE)
                            .text(R.id.tv_money_type, "QC")
                            .text(R.id.tv_money, data)
                            .clicked(R.id.tv_bill_record, view -> ARouter.getInstance().build(RConfig.APP_BILLIND_RECORD).withString(Constans.MONEY_TYPE, "QC").navigation())
                            .clicked(R.id.btn_exchange, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    ARouter.getInstance().build(RConfig.APP_EXCHANGE).navigation();

                                }
                            });
                } else {
                    injector.visibility(R.id.tv_trading_record, View.VISIBLE)
                            .visibility(R.id.btn_recharge, View.VISIBLE)
                            .visibility(R.id.btn_with_draw, View.VISIBLE)
                            .visibility(R.id.btn_exchange, View.GONE)
                            .image(R.id.iv_image_type, injector.pos() == 0 ? App.INSTANCE.getResources().getDrawable(R.mipmap.app_icon_jinbi) : App.INSTANCE.getResources().getDrawable(R.mipmap.app_icon_usdt))
                            .text(R.id.tv_money_type, injector.pos() == 0 ? "金币" : "USDT")
                            .text(R.id.tv_money, data)
                            .clicked(R.id.tv_bill_record, view -> ARouter.getInstance().build(RConfig.APP_BILLIND_RECORD).withString(Constans.MONEY_TYPE, injector.pos() == 0 ? "金币" : "USDT").navigation())
                            .clicked(R.id.tv_trading_record, view -> ARouter.getInstance().build(RConfig.APP_ASSET_RECORD).withString(Constans.MONEY_TYPE, injector.pos() == 0 ? "金币" : "USDT").navigation())
                            .clicked(R.id.btn_recharge, view -> ARouter.getInstance().build(RConfig.APP_RECHARGE).withString(Constans.MONEY_TYPE, injector.pos() == 0 ? "金币" : "USDT").navigation())
                            .clicked(R.id.btn_with_draw, view -> ARouter.getInstance().build(RConfig.APP_WITHDRAW).withString(Constans.MONEY_TYPE, injector.pos() == 0 ? "金币" : "USDT").navigation());
                }

            }
        }).attachTo(recyclerView).updateData(new ArrayList());
//        ModelRx2DataSource<String> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<String>)
//                page -> Flowable.range(0, 20)
//                        .map(i -> i + "")
//                        .toList().toFlowable(), 20);
//        MVCSmartHelper<List<String>> mvcHelper = new MVCSmartHelper<>(smartRefreshLayout);
//        mvcHelper.setDataSource(dataSource);
//        mvcHelper.setAdapter(slimAdapter);
//        mvcHelper.refresh();
    }

    public void updateQC(HttpData httpData, UserBean userBean) {
        smartRefreshLayout.finishRefresh();
        if (httpData.getStatus() == HttpConfig.OK) {
            moneys.clear();
            moneys.add(userBean.getBalance_usdt());
            moneys.add(userBean.getBalance_cny());
            moneys.add(userBean.getBalance_pb());
            slimAdapter.updateData(moneys);
        } else {
            ToastUtil.showShort(httpData.msg);
        }
//        tvQcTotalMoney.setText(userBean.getBalance_pb());
//        tvUsdtTotalMoney.setText(userBean.getBalance_cny());
    }
}
